package com.zw.a.basic.a10.demo05_arraylist;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

//案例: 演示ArrayList集合存储字符串并遍历.
public class Demo01 {
    public static void main(String[] args) {
        //1.定义ArrayList集合, 存储3个字符串信息, 分别是: "hello", "world", "java".
        ArrayList<String> list = new ArrayList<>();
        list.add("hello");
        list.add("world");
        list.add("java");
        //2.通过三种方式, 遍历上述的集合.
        // 提示: 迭代器, 普通for, 增强for.
        //方式一: 迭代器
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String s = it.next();
            System.out.println(s);
        }
        System.out.println("----------------------");

        //方式二: 普通for
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            System.out.println(s);
        }
        System.out.println("-------------------------");

        //方式三: 增强for, 掌握.
        for (String s : list) {
            System.out.println(s);
        }
        System.out.println("-------------------------");

        //方式四: 列表迭代器, 掌握.
        ListIterator<String> lit = list.listIterator();
        while (lit.hasNext()) {
            String s = lit.next();
            System.out.println(s);
        }
        System.out.println("-------------------------");

        //方式五: 把集合转成数组, 然后遍历.  Collection#toArray();    了解.
        Object[] objs = list.toArray();
        for (Object obj : objs) {
            System.out.println(obj);
        }
    }
}
